package com.gcb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.PhoneUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcb.common.enums.*;
import com.gcb.common.utils.OrderUtils;
import com.gcb.domain.dto.OrderStaffQueryDTO;
import com.gcb.domain.dto.OrderStaffSaveDTO;
import com.gcb.domain.entity.OrderStaff;
import com.gcb.domain.vo.OrderStaffVO;
import com.gcb.mapper.OrderStaffMapper;
import com.gcb.service.IOrderStaffService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class OrderStaffServiceImpl extends ServiceImpl<OrderStaffMapper, OrderStaff> implements IOrderStaffService {

    @Resource
    private OrderStaffMapper orderStaffMapper;


    @Override
    public void add(OrderStaffSaveDTO orderStaffSaveDto, Long currentUserId) {
        OrderStaff orderStaff = new OrderStaff();
        BeanUtil.copyProperties(orderStaffSaveDto, orderStaff);
        //订单号
        orderStaff.setOrderNo(OrderUtils.generateOrderNo("S"));

        // 设置订单状态 当前没有审核流程，直接审核通过，状态为进行中
        orderStaff.setState(OrderStateEnum.UNDERWAY.getCode());

        orderStaff.setCreateBy(currentUserId.toString());
        orderStaff.setCreateTime(new Date());
        orderStaff.setUpdateBy(currentUserId.toString());
        orderStaff.setUpdateTime(new Date());
        this.save(orderStaff);
    }

    @Override
    public void update(OrderStaffSaveDTO orderStaffSaveDto, Long currentUserId) {
        OrderStaff orderStaff = new OrderStaff();
        BeanUtil.copyProperties(orderStaffSaveDto, orderStaff);
        orderStaff.setUpdateBy(currentUserId.toString());
        orderStaff.setUpdateTime(new Date());
        this.updateById(orderStaff);
    }


    @Override
    public OrderStaffVO getDetail(Long id) {
        OrderStaffVO orderStaffVO = orderStaffMapper.getDetail(id);
        handleVO(orderStaffVO);
        return orderStaffVO;
    }


    /**
     * 列表
     *
     * @param queryDTO
     * @return
     */
    @Override
    public List<OrderStaffVO> list(OrderStaffQueryDTO queryDTO) {
        List<OrderStaffVO> orderStaffVOList = orderStaffMapper.list(queryDTO);
        for (OrderStaffVO orderStaffVO : orderStaffVOList) {
            handleVO(orderStaffVO);
        }
        return orderStaffVOList;
    }

    /**
     * 获取枚举值
     *
     * @param orderStaffVO
     */
    public void handleVO(OrderStaffVO orderStaffVO) {
        // 订单状态
        orderStaffVO.setStateStr(OrderStateEnum.getInfo(orderStaffVO.getState()));
        // 资格证书
        if (orderStaffVO.getCredentialsType() != null) {
            orderStaffVO.setCredentialsTypeStr(CredentialsTypeEnum.getInfo(orderStaffVO.getCredentialsType()));
        }
        // 证书等级
        if (orderStaffVO.getCredentialsLevel() != null) {
            orderStaffVO.setCredentialsLevelStr(CredentialsLevelEnum.getInfo(orderStaffVO.getCredentialsLevel()));
        }
        // 性别
        if (orderStaffVO.getSex() != null) {
            orderStaffVO.setSexStr(SexTypeEnum.getInfo(orderStaffVO.getSex()));
        }
        // 创建类型
        orderStaffVO.setCreateTypeStr(CreateTypeEnum.getInfo(orderStaffVO.getCreateType()));
        if (orderStaffVO.getMobile() != null) {
            orderStaffVO.setMobile(PhoneUtil.hideBetween(orderStaffVO.getMobile()).toString());
        }
    }

    @Override
    public void batchUpdateState(Long[] ids, Integer state, Long currentUserId) {
        orderStaffMapper.batchUpdateState(ids, state, currentUserId);
    }


}
